package com.cortex.papaya.view;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ViewFlipper;

import com.cortex.papaya.activities.MainActivity;
import com.cortex.papaya.listener.SlidViewListener;
import com.cortex.papaya.listener.SlidViewListener.OnSlidFingerListener;
import com.cortex.papaya.listener.SlidViewListener.SlidDirection;
import com.cortex.papaya.view.PapayaRowSearch.ISlidFinger;
import com.cortex.papayasearch.HelpData;
import com.cortex.papayasearch.R;

public class PapayaHelpView extends RelativeLayout implements AnimationListener{

	private ViewFlipper flipper;
	private Button btnNext,btnBack;
	private CortexTextView tvSkipAll;
	private Animation in;
	private Animation out;
	public final static String KEY_SHOW_HELP = "showHelp";
	private HelpData helpData;
	private int page;
	public PapayaHelpView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public PapayaHelpView(Context context) {
		super(context);
	}
	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();
		init();
	}
	private void init(){
		flipper = (ViewFlipper)findViewById(R.id.flipperHelp);
		btnNext = (Button)findViewById(R.id.btnNext);
		btnBack = (Button)findViewById(R.id.btnBack);
		tvSkipAll = (CortexTextView)findViewById(R.id.tvSkipAll);
		initHandlers();
	}
	private void initHandlers(){
		btnNext.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				View currentView,lastView;
				currentView = flipper.getCurrentView();
				lastView = flipper.getChildAt(flipper.getChildCount() - 1);
				if(currentView == lastView){
					hide();
					helpData.setPageDisplayed(page, true);
					return;
				}
				if(currentView != lastView){
					createAnimation(R.anim.push_right_in, R.anim.push_right_out);
					flipper.showNext();
					currentView = flipper.getCurrentView();
				}
				if(currentView == lastView){
					btnNext.setText(getResources().getString(R.string.close));
				}
			}
		});
		btnBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				View currentView,firstView;
				currentView = flipper.getCurrentView();
				firstView = flipper.getChildAt(0);
				if(currentView  != firstView){
					createAnimation(R.anim.push_left_in, R.anim.push_left_out);
					flipper.showPrevious();

				}
				if(currentView != firstView){
					btnNext.setText(getResources().getString(R.string.next));
				}

			}
		});
		tvSkipAll.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				helpData.setSkipAll(true);
				hide();
			}
		});
	}
	public void setHelpData(HelpData helpData){
		this.helpData = helpData;
	}
	public void showHelp(int page){
		this.page = page;
		if(helpData.isSkipAll() || helpData.getPage(page).isDisplayed()){
			return;
		}
		int[] resourcesImages = helpData.getPage(page).getViews();
		initView(resourcesImages);
		LayoutInflater inflater = LayoutInflater.from(getContext());
		for (int i = 0; i < resourcesImages.length; i++) {
			ImageView image = (ImageView) inflater.inflate(R.layout.view_image_help,null);
			image.setBackgroundResource(resourcesImages[i]);
			flipper.addView(image);

		}
		setVisibility(VISIBLE);
	}
	private void initView(int... resourcesImages){
		flipper.removeAllViews();
		btnNext.setText(getResources().getString(R.string.next));
		if(resourcesImages.length == 1){
			btnNext.setText(getResources().getString(R.string.close));
		}
	}
	public void hide(){
		setVisibility(GONE);
	}
	private void createAnimation(Integer animationIn,Integer animationOut){
		// create animation in and animation out
		PapayaHelpView me = PapayaHelpView.this;
		in = AnimationUtils.loadAnimation(getContext(), animationIn);
		out = AnimationUtils.loadAnimation(getContext(), animationOut);
		// add animation in and out to this activity
		in.setAnimationListener(me);
		out.setAnimationListener(me);
		// add animation in and out to flipper
		flipper.setInAnimation(in);
		flipper.setOutAnimation(out);
	}

	@Override
	public void onAnimationEnd(Animation animation) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onAnimationRepeat(Animation animation) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onAnimationStart(Animation animation) {
		// TODO Auto-generated method stub

	}
}
